package dragon.modules.system.config.intercept;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import dragon.core.exception.ExceptionHandler;
import dragon.core.io.XMLHandler;
import dragon.core.util.FilePath;
import dragon.model.bizObject.BizObject;
import dragon.model.intercept.Interceptor;
import dragon.modules.system.config.dao.SysConfig;
import ejp.Database;
import ejw.ServerInterface;

/**
 * 实现功能：系统参数配置截器类
 */
public class ConfigInterceptor extends Interceptor {

	@Override
	public int beforeLoadData(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif) throws ExceptionHandler {
		List<Map<String, Object>> list = XMLHandler.readXML(FilePath.webappxml, "param", null, false);
		for (Map<String, Object> m : list) {
			m.put("ID", m.get("id"));
			m.put("VALUE", m.get("value"));
			m.put("DESCRIPTION", m.get("description"));
			m.remove("id");
			m.remove("value");
			m.remove("description");
		}

		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("rows", list);
		JSONObject jsonObject = new JSONObject();
		jsonObject.putAll(jsonMap);

		output(sif, jsonObject.toJSONString());

		list = null;
		jsonObject = null;
		jsonMap = null;

		return EXCLUDE_ALL;
	}

	@Override
	public int beforeAdd(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>(1);
		Map<String, String> map = new HashMap<String, String>(3);
		map.put("tagName", "param");
		map.put("id", parameterMap.get("ID"));
		map.put("value", parameterMap.get("VALUE"));
		map.put("description", parameterMap.get("DESCRIPTION"));
		list.add(map);

		XMLHandler.insertNode(FilePath.webappxml, "parameters", null, list);

		map = null;
		list = null;

		SysConfig.loadConfig(sif.getServletContext());// 重新加载到内存

		return EXCLUDE_ALL;
	}

	@Override
	public int beforePreMod(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif) throws ExceptionHandler {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ID", parameterMap.get("ID"));
		map.put("VALUE", parameterMap.get("VALUE"));
		map.put("DESCRIPTION", parameterMap.get("DESCRIPTION"));
		bizObject.fillFields(map);
		map = null;
		return EXCLUDE_ALL;
	}

	@Override
	public int beforeMod(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>(1);
		Map<String, String> map = new HashMap<String, String>(3);
		map.put("tagName", "param");
		map.put("id", parameterMap.get("ID"));
		map.put("value", parameterMap.get("VALUE"));
		map.put("description", parameterMap.get("DESCRIPTION"));
		list.add(map);

		XMLHandler.updateNode(FilePath.webappxml, list);

		map = null;
		list = null;

		SysConfig.loadConfig(sif.getServletContext());// 重新加载到内存

		return EXCLUDE_ALL;
	}

	@Override
	public int beforeDel(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		try {
			JSONArray rows = (JSONArray) new JSONParser().parse(parameterMap.get("rows"));
			JSONObject jsonObj = null;
			for (Object obj : rows) {
				jsonObj = (JSONObject) obj;
				XMLHandler.deleteNode(FilePath.webappxml, "param", jsonObj.get("ID").toString());
				obj = null;
			}
			jsonObj = null;
			rows = null;
		} catch (ParseException e) {
			throw new ExceptionHandler(e);
		}

		SysConfig.loadConfig(sif.getServletContext());// 重新加载到内存

		return EXCLUDE_ALL;
	}

}
